# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Contributor: 6543 <6543@obermui.de>
# Contributor: techknowlogick <techknowlogick@gitea.com>
# Maintainer: 6543 <6543@obermui.de>
pkgname=gitea
pkgver=1.21.9
pkgrel=1
pkgdesc="Self-hosted Git service written in Go"
url="https://about.gitea.com/"
arch="all"
license="MIT"
depends="git git-lfs gnupg"
makedepends="go"
checkdepends="bash openssh openssh-keygen sqlite tzdata"
install="$pkgname.pre-install"
pkgusers="gitea"
pkggroups="www-data"
subpackages="$pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://dl.gitea.com/gitea/$pkgver/gitea-src-$pkgver.tar.gz
	$pkgname.initd
	$pkgname.ini
	"
builddir="$srcdir/gitea-src-$pkgver"
options="!check net" # broken with GIT_CEILING

# secfixes:
#   1.21.3-r0:
#     - CVE-2023-48795
#   1.17.3-r0:
#     - CVE-2022-42968
#     - CVE-2022-32149
#   1.16.7-r0:
#     - CVE-2022-30781
#   1.16.3-r0:
#     - CVE-2022-27313
#   1.14.6-r0:
#     - CVE-2020-26160
#   1.13.7-r0:
#     - CVE-2021-29272
#   1.13.6-r0:
#     - CVE-2021-29134
#   1.13.4-r0:
#     - CVE-2021-28378
#   1.13.2-r0:
#     - CVE-2021-3382
#   1.11.2-r0:
#     - CVE-2021-45327
#   1.5.2-r0:
#     - CVE-2021-45326
#   1.5.1-r0:
#     - CVE-2021-45331
#     - CVE-2021-45329

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

# Skip tests for archs that fail unrelated in CI
case "$CARCH" in
	s390x|x86|armhf|armv7) options="$options !check" ;;
esac

build() {
	# XXX: LARGEFILE64
	export CGO_CFLAGS="$CFLAGS -O2 -D_LARGEFILE64_SOURCE"
	export TAGS="tidb sqlite sqlite_unlock_notify"
	export GITEA_VERSION="$pkgver"
	export EXTRA_GOFLAGS="$GOFLAGS"
	export CGO_LDFLAGS="$LDFLAGS"
	unset LDFLAGS
	## make FHS compliant
	LDFLAGS="$LDFLAGS -X code.gitea.io/gitea/modules/setting.CustomConf=/etc/gitea/app.ini"
	LDFLAGS="$LDFLAGS -X code.gitea.io/gitea/modules/setting.AppWorkPath=/var/lib/gitea/"
	LDFLAGS="$LDFLAGS -X code.gitea.io/gitea/modules/setting.StaticRootPath=/usr/share/webapps/gitea/"
	export LDFLAGS

	make -j1 backend
}

check() {
	local home="$srcdir"/home
	mkdir -p "$home"
	install -d -m700 "$home"/.ssh
	touch "$home"/.gitconfig

	env GITEA_ROOT="$home" HOME="$home" GITEA_WORK_DIR="$(pwd)" timeout -s ABRT 20m make -j1 test-sqlite
	## "make test" - modified (exclude broken tests)
	## 'code.gitea.io/gitea/modules/migrations': github hase rate limits! 403 API
	local tests=$(go list ./... | grep -v /vendor/ | \
		grep -v 'code.gitea.io/gitea/modules/migrations' | \
		grep -v 'code.gitea.io/gitea/modules/charset' | \
		grep -v 'code.gitea.io/gitea/models/migrations' | \
		grep -v 'code.gitea.io/gitea/services/migrations' | \
		grep -v 'code.gitea.io/gitea/integrations')
	env GITEA_CONF="$PWD/tests/sqlite.ini" GITEA_ROOT="$home" HOME="$home" GO111MODULE=on go test -mod=vendor -tags='sqlite sqlite_unlock_notify' $tests

}

package() {
	for dir in $pkgname $pkgname/git $pkgname/data $pkgname/db $pkgname/custom; do
		install -d -m 750 -o gitea -g www-data \
			"$pkgdir"/var/lib/$dir
	done

	install -d -m 755 -o gitea -g www-data "$pkgdir"/var/log/$pkgname

	install -D -m 755 $pkgname "$pkgdir"/usr/bin/$pkgname

	install -D -m 644 -o gitea -g www-data "$srcdir"/gitea.ini \
		"$pkgdir"/etc/$pkgname/app.ini
	chown gitea:www-data "$pkgdir"/etc/$pkgname

	install -d -m 755 "$pkgdir"/usr/share/webapps/$pkgname
	cp -r options public templates "$pkgdir"/usr/share/webapps/$pkgname/

	install -D -m 755 "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/$pkgname
}

sha512sums="
1c62bf899e45e355a5653c95f34005495df31354c53e1e3197443526d92e0c5fcbbcb808277503c2efb358f9c5390ce6f6d4859b3ccc3e9c7d4eecd7caecb6b0  gitea-1.21.9.tar.gz
71c767f9e851d99e6d87032faa833617ded28a292507c492bce63d0b87208819aa9ccbf8301d885da14901a4bdb93b21859883eaea78f980e8c8deb63ae8e57a  gitea.initd
431184faffa8996873d92d7b0d16bc4b1a0178d264cd2928d1f49b13ad3e6470d9ede7a18c12112deeeb38f0647ccc0b012e98bcbd96e7b8496a3dc18f5b1fb7  gitea.ini
"
